//
//  MainNavigationController.swift
//  Inpro
//
//  Created by 彭灿 on 16/9/13.
//  Copyright © 2016年 Roble. All rights reserved.
//

import UIKit

class MainNavigationController: UINavigationController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //隐藏默认的 NavigationBar，为了自定义Bar
        navigationBar.isHidden = true
    }
    

    ///重写 push 方法, 所有的 push 动作 都会调用此方法
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        print(viewController)
        
        // 如果不是栈底控制器才需要处理
        if childViewControllers.count > 0 {
            // 隐藏底部的 TabBar
            viewController.hidesBottomBarWhenPushed = true
            
            if let vc = viewController as? MainBaseViewController {
                var title = "返回"
                
                if childViewControllers.count == 1{
                    title = childViewControllers.first?.title ?? "返回"
                }
                vc.navItem.leftBarButtonItem = UIBarButtonItem(title: title,target: self, action: #selector(popToParent))
            }
        }
        
        super.pushViewController(viewController, animated: true)
    }
    
    /// 返回到上一级控制器
    @objc public func popToParent() {
        popViewController(animated: true)
    }
}
